package com.yly.doctorapp.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;

import com.yly.doctorapp.R;
import com.yly.doctorapp.listener.PrescriptionListener;
import com.yly.doctorapp.listener.PrescriptionOnclickListener;
import com.yly.doctorapp.ui.prescription.PrescriptionDetailActivity;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PrescriptionDetailItemAdapter extends BaseAdapter {
    private Context mContext;
    private boolean isCanEdit = false;
    private PrescriptionListener mListener;
    private PrescriptionOnclickListener mOnclickListener;
    private int mTouchItemPosition = -1;

    public PrescriptionOnclickListener getmOnclickListener() {
        return mOnclickListener;
    }

    public void setmOnclickListener(PrescriptionOnclickListener mOnclickListener) {
        this.mOnclickListener = mOnclickListener;
    }

    private int isNum = 0;

    public int isNum() {
        return isNum;
    }

    public void setNum(int num) {
        isNum = num;
    }

    public PrescriptionListener getmListener() {
        return mListener;
    }

    public void setmListener(PrescriptionListener mListener) {
        this.mListener = mListener;
    }

    public PrescriptionDetailItemAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public boolean isCanEdit() {
        return isCanEdit;
    }

    public void setCanEdit(boolean canEdit) {
        isCanEdit = canEdit;
    }

    @Override
    public int getCount() {
        return PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.layout_ppdetailitem, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (null != PrescriptionDetailActivity.mPrescriptionDetailItemBeanList && PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.size() > 0) {
            holder.editYao.setText(PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.get(position).getName());
            String unit = "";
            if (StringUtils.isEmpty(PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.get(position).getUnit())) {
                unit = "单位:g";
                PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.get(position).setUnit("g");
            } else {
                unit = "单位:" + PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.get(position).getUnit();
            }
            holder.editDanwei.setText(unit);
            holder.editNum.setText(PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.get(position).getNum() + "");
            holder.editYao.setSelection(holder.editYao.getText().toString().length());
            holder.editNum.setSelection(holder.editNum.getText().toString().length());
            holder.editDanwei.setSelection(holder.editDanwei.getText().toString().length());
            holder.editYao.setFocusable(false);
            holder.editYao.setFocusableInTouchMode(false);
            holder.editNum.setFocusable(true);
            holder.editNum.setFocusableInTouchMode(true);
            holder.editDanwei.setFocusable(false);
            holder.editDanwei.setFocusableInTouchMode(false);
            if (isCanEdit()) {
                holder.editYao.setEnabled(true);
                holder.editNum.setEnabled(true);
                holder.editDanwei.setEnabled(false);
                holder.editNum.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        mTouchItemPosition = position;
                        return false;
                    }
                });
                holder.editYao.setBackgroundResource(R.drawable.line_returnbg);
                holder.editNum.setBackgroundResource(R.drawable.line_returnbg);
                holder.editDanwei.setBackground(null);
                holder.ivDelete.setVisibility(View.VISIBLE);
                holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.size() > 0) {
                            PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.remove(position);
                            mListener.callBack();
                        }
                    }
                });
                getChangeData(holder, position);
            } else {
                holder.editYao.setEnabled(false);
                holder.editYao.setFocusable(false);
                holder.editYao.setFocusableInTouchMode(false);
                holder.editNum.setEnabled(false);
                holder.editNum.setFocusable(false);
                holder.editNum.setFocusableInTouchMode(false);
                holder.editDanwei.setEnabled(false);
                holder.editDanwei.setFocusable(false);
                holder.editDanwei.setFocusableInTouchMode(false);
                holder.editYao.setBackground(null);
                holder.editNum.setBackground(null);
                holder.editDanwei.setBackground(null);
                holder.ivDelete.setVisibility(View.GONE);
            }
        }
        if (mTouchItemPosition == position) {
            holder.editNum.requestFocus();
            holder.editNum.setFocusable(true);
            holder.editNum.setFocusableInTouchMode(true);
            holder.editNum.setSelection(holder.editNum.getText().length());
        } else {
            holder.editNum.clearFocus();
            holder.editNum.setFocusable(false);
            holder.editNum.setFocusableInTouchMode(false);
        }
        return convertView;
    }

    private void getChangeData(final ViewHolder holder, final int mPosition) {
        holder.editNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!StringUtils.isEmpty(s.toString())) {
                    PrescriptionDetailActivity.mPrescriptionDetailItemBeanList.get(mPosition).setNum(s.toString());
//                    mListener.callBack();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        holder.editYao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.editYao.setFocusable(false);
                holder.editYao.setFocusableInTouchMode(false);
                holder.editNum.setFocusable(false);
                holder.editNum.setFocusableInTouchMode(false);
                holder.editDanwei.setFocusable(false);
                holder.editDanwei.setFocusableInTouchMode(false);
                mOnclickListener.callBack(0, mPosition);
            }
        });
        holder.editNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.editYao.setFocusable(false);
                holder.editYao.setFocusableInTouchMode(false);
                holder.editNum.setFocusable(true);
                holder.editNum.setFocusableInTouchMode(true);
                holder.editDanwei.setFocusable(false);
                holder.editDanwei.setFocusableInTouchMode(false);
            }
        });
        holder.editDanwei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mOnclickListener.callBack(2, mPosition);
            }
        });
    }

    class ViewHolder {
        @BindView(R.id.edit_yao)
        EditText editYao;
        @BindView(R.id.edit_num)
        EditText editNum;
        @BindView(R.id.edit_danwei)
        EditText editDanwei;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
